﻿using System;
using System.Globalization;
using System.Windows.Forms;

namespace WindowPlacer
{
    public partial class SettingsForm : Form
    {
        public IntPtr BotWindowHandler;
        private readonly CultureInfo _myCulture = CultureInfo.InvariantCulture;

        public SettingsForm(IntPtr bwt)
        {
            InitializeComponent();
            BotWindowHandler = bwt;
        }

        private void SettingsFormLoad(object sender, EventArgs e)
        {
            // Poe Informations
            cb_resizeandreplace.Checked = Main.MyWindowsPosition.ResizeAndReplacePoeWindow;
            txt_poeX.Text = Main.MyWindowsPosition.PoeX.ToString(_myCulture);
            txt_poeY.Text = Main.MyWindowsPosition.PoeY.ToString(_myCulture);
            txt_poeWidth.Text = Main.MyWindowsPosition.PoeWidth.ToString(_myCulture);
            txt_poeHeight.Text = Main.MyWindowsPosition.PoeHeight.ToString(_myCulture);

            // Bot Informations
            txt_botX.Text = Main.MyWindowsPosition.BotX.ToString(_myCulture);
            txt_botY.Text = Main.MyWindowsPosition.BotY.ToString(_myCulture);
            txt_botWidth.Text = Main.MyWindowsPosition.BotWidth.ToString(_myCulture);
            txt_botHeight.Text = Main.MyWindowsPosition.BotHeight.ToString(_myCulture);
        }

        private void SettingsFormClosing(object sender, EventArgs e)
        {
            // Save Windows Infos
            Main.MyWindowsPosition.ResizeAndReplacePoeWindow = cb_resizeandreplace.Checked;
            Main.MyWindowsPosition.BotX = Convert.ToInt32(txt_botX.Text);
            Main.MyWindowsPosition.BotY = Convert.ToInt32(txt_botY.Text);
            Main.MyWindowsPosition.BotWidth = Convert.ToInt32(txt_botWidth.Text);
            Main.MyWindowsPosition.BotHeight = Convert.ToInt32(txt_botHeight.Text);
            Main.MyWindowsPosition.PoeX = Convert.ToInt32(txt_poeX.Text);
            Main.MyWindowsPosition.PoeY = Convert.ToInt32(txt_poeY.Text);
            Main.MyWindowsPosition.PoeWidth = Convert.ToInt32(txt_poeWidth.Text);
            Main.MyWindowsPosition.PoeHeight = Convert.ToInt32(txt_poeHeight.Text);

            Main.MyWindowsPosition.SaveInformations(); 
        }

        private void BtGrabBotCurrentInfosClick(object sender, EventArgs e)
        {
            Helpers.RECT rect;
            Helpers.GetWindowRect(BotWindowHandler, out rect);
            txt_botX.Text = rect.Left.ToString(_myCulture);
            txt_botY.Text = rect.Top.ToString(_myCulture);
            txt_botWidth.Text = (rect.Right - rect.Left + 1).ToString(_myCulture);
            txt_botHeight.Text = (rect.Bottom - rect.Top + 1).ToString(_myCulture);
        }

        private void BtGrabPoEWindowInfoClick(object sender, EventArgs e)
        {
            Helpers.RECT rect;
            Helpers.GetWindowRect(Loki.Game.LokiPoe.ClientWindowHandle, out rect);
            txt_poeX.Text = rect.Left.ToString(_myCulture);
            txt_poeY.Text = rect.Top.ToString(_myCulture);
            txt_poeWidth.Text = (rect.Right - rect.Left + 1).ToString(_myCulture);
            txt_poeHeight.Text = (rect.Bottom - rect.Top + 1).ToString(_myCulture);
        }

        private void CbResizeandreplaceCheckedChanged(object sender, EventArgs e)
        {
            Main.MyWindowsPosition.ResizeAndReplacePoeWindow = cb_resizeandreplace.Checked;
        }

        private void TxtPoeWidthChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txt_poeWidth.Text) > 816)
                txt_poeWidth.Text = 816.ToString(_myCulture);
        }

        private void TxtPoeHeightChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txt_poeHeight.Text) > 638)
                txt_poeHeight.Text = 638.ToString(_myCulture);
        }


    }
}
